import React, { Component } from 'react'
import { connect } from 'dva';
import Link from 'umi/link';
import { Table, Divider, Modal, message } from 'antd';
import Form from './components/SearchForm';
import { deleteProblem } from './problemService';
import { checkInit } from '@/utils/util';

export interface Props {
  dispatch?: Function,
  children?: React.ReactNode,
  loading: boolean,
  list: {
    data: Array<any>,
    dataCnt: number
  },
  buttons?: Array<any>,
  dict?: object
}

const confirm = Modal.confirm;
const ONE_PAGE_SIZE = 10;

@connect(({ unit_template_question, loading, menu }) => ({
  list: unit_template_question.list || {},
  dict: unit_template_question.dict,
  buttons: menu.buttons,
  loading: loading.effects['unit_template_question/queryPage']
}))
class Position extends Component<Props, any> {
  constructor(props) {
    super(props);
    this.initCurrent = this.initCurrent.bind(this);
    this.delConfirm = this.delConfirm.bind(this);
  }
  state = {
    current: 1,
    userIdList: []
  };

  queryData(offset = 1, size = ONE_PAGE_SIZE) {
    const { dispatch } = this.props;
    dispatch({
      type: 'unit_template_question/queryPage',
      payload: {
        pageNum: offset,
        pageSize: size,
        checkType: '1'
      },
    });
  }

  initCurrent() {
    this.setState({
      current: 1
    })
  }

  delConfirm(data) { // 删除
    let that = this;
    confirm({
      title: '删除',
      content: '是否删除？',
      onOk() {
        let tipMsg = message.loading('');
        return deleteProblem(data.id).then(() => {
          tipMsg();
          message.success('操作成功', 1.5);
          that.queryData();
        }, tipMsg);
      },
      onCancel() {},
      okText: "确认",
      cancelText: "取消"
    });
  }

  componentDidMount() {
    const { dispatch } = this.props;
    checkInit() ? dispatch({ type: `unit_template_question/initCond` }) : this.queryData();
  }

  render() {
    const { buttons } = this.props;
    const btnList = {};
    if(Array.isArray(buttons)) {
      buttons.forEach(item => {
        btnList[item.name] = item.name
      });
    }
    const { data = [], dataCnt } = this.props.list;
    const ans = data && Array.isArray(data) ? data.map((item, index) => ({
      key: index,
      id: item.id,
      name: item.name,
      typeName: item.typeName ? item.typeName : ' '
      })
    ) : [];
    const pagination = {
      total: dataCnt,
      showTotal: total => `共 ${total} 条`,
      showQuickJumper: true,
      pageSize: ONE_PAGE_SIZE,
      onChange: (current) => {
        this.setState({ current });
        this.queryData(current)
      },
    };
    const columns = [{
        title: '问题名称',
        dataIndex: 'name',
      }, {
        title: '问题类型',
        dataIndex: 'typeName',
      }, {
        title: '操作', key: 'x', render: (record, text, index) => {
        return (
          <div style={{display: 'flex'}}>
            {
              btnList['编辑'] ? <>
                <Link to={`/data/unit_template_question/${record.id}/update`}>编辑</Link>
                <Divider type="vertical" />
              </> : null
            }
            {
              btnList['查看'] ? <>
                <Link to={`/data/unit_template_question/${record.id}/details`}>查看</Link>
                <Divider type="vertical" />
              </> : null
            }
            {
              btnList['删除'] ? <>
                <span
                  onClick = {() => {
                    this.delConfirm(record)
                  }}
                  style={{'cursor': 'pointer', 'color': '#0880DF'}}>
                  删除
                </span>
              </> : null
            }
          </div>
        )}
      }];
    return (
      <div>
        <Form current={ this.state.current } initCurrent={this.initCurrent} new={!!btnList['新增']}/>
        <Table
          columns={columns}
          pagination={pagination}
          dataSource={ans}
          bordered
          loading={this.props.loading}
        />
      </div>
    );
  }
}

export default Position;
